//
//  RootViewController.m
//  SSSMerchant
//
//  Created by Darker on 2017/3/24.
//  Copyright © 2017年 xnxin. All rights reserved.
//

#import "RootViewController.h"
#import "SSSGuideView.h"
#import "SSSLoginViewController.h"
#import <QMUIKit/QMUIKit.h>
#import "SSSHomeViewController.h"
#import "SSSPersonCenterController.h"
#import "SSSMyIncomeViewController.h"


@interface RootViewController ()

/** <#注释#> */
@property (nonatomic, strong) QMUITabBarViewController *sssTabBarController;

@end

#define kApplicationHasShowIntroViewKey @"ApplicationHasShowIntroViewKey"
#define kApplicationVersion             @"ApplicationVersion"

@implementation RootViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"changeLogin" object:nil];
}

// 切换容器视图到Login
- (void)changeLoginPushController:(NSNotification *)notification
{
    [self.view addSubview:self.childViewControllers[0].view];
    [self.childViewControllers[1].view removeFromSuperview];
    [self.sssTabBarController removeFromParentViewController];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeLoginPushController:) name:@"changeLogin" object:nil];
    // 初始化
    [self initializeInterface];
    
    
}
#pragma mark - Init
- (void)initializeInterface
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self addChildViewControllers];
    
    [self.view addSubview:self.childViewControllers[0].view];
    // 加载初始化
    [self setupAppearance];
    // 检查版本更新
    //[SlwyUpdateApp CheckTheVersionUpdate];
}

// 加载初始化
- (void)setupAppearance
{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    //💖 获取当前版本
    NSString *curVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleVersion"];
    //💖 获取之前存储版本
    NSString *lastVersion = [userDefault objectForKey:kApplicationVersion];
    //💖 有新版本, 需要显示引导页
    if (![curVersion isEqualToString:lastVersion])
    {
        [userDefault setBool:NO
                      forKey:kApplicationHasShowIntroViewKey];
        [userDefault setObject:[NSBundle mainBundle].infoDictionary[@"CFBundleVersion"]
                        forKey:kApplicationVersion];
        // 加载引导页
        [self addGuidePageView];
        
    }else{
        
    }
}


// 加载引导页
- (void)addGuidePageView
{
    NSArray *imageArray = @[[UIImage imageNamed:@"引导页1"],
                            [UIImage imageNamed:@"引导页2"],
                            [UIImage imageNamed:@"引导页3"]];
    SSSGuideView *guidePage = [[SSSGuideView alloc] dh_initWithFrame:CGRectMake(0, 0, KSCREEN_Width, KSCREEN_Height)
                                                                imageArray:imageArray
                                                            buttonIsHidden:YES];
    [self.view addSubview:guidePage];
}
// 添加两个子控制器在容器中
- (void)addChildViewControllers
{
    // 登录
    SSSLoginViewController *sssLoginVC = [SSSLoginViewController new];
    [sssLoginVC SSSLoginViewControllerBlock:^{
        // 登录成功回调 并默认首页展示
        [self loginButtonBlockAddTabbarController];
       // [sssLoginVC.view removeFromSuperview];
    }];
    [self addChildViewController:[[QMUINavigationController alloc]initWithRootViewController:sssLoginVC]];
}
/** 登录按钮点击之后，初始化tabbar控制器 并且 添加到容器中，再设置其显示页为当前页面。最后添加到容器的视图上。**/
- (void)loginButtonBlockAddTabbarController
{
    
    SSSHomeViewController *home=[SSSHomeViewController new];
    home.hidesBottomBarWhenPushed = NO;
    QMUINavigationController *homeNav= [[QMUINavigationController alloc]
                                        initWithRootViewController:home];
    homeNav.tabBarItem.title = @"首页";
    homeNav.tabBarItem.image = [UIImage imageNamed:@"icon_home_06"];
    
    UIViewController *inCome = [SSSMyIncomeViewController new];
    inCome.hidesBottomBarWhenPushed = NO;
    QMUINavigationController *inComeNav = [[QMUINavigationController alloc]
                                           initWithRootViewController:inCome];
    inComeNav.tabBarItem.title = @"我的收益";
    inComeNav.tabBarItem.image = [UIImage imageNamed:@"icon_home_07"];
    
    SSSPersonCenterController *personal = [SSSPersonCenterController new];
    personal.hidesBottomBarWhenPushed = NO;
    QMUINavigationController *personalNav = [[QMUINavigationController alloc]
                                             initWithRootViewController:personal];
    personalNav.tabBarItem.title = @"个人中心";
    personalNav.tabBarItem.image = [UIImage imageNamed:@"icon_home_08"];
    
    QMUITabBarViewController *tabVc = [QMUITabBarViewController new];
    tabVc.viewControllers = @[homeNav, inComeNav, personalNav];

    self.view.window.rootViewController = tabVc;
}


@end
